#!/usr/bin/env python
import sys
from os.path import isfile

from PyQt4.QtCore import *
from PyQt4.QtGui import *


from ui_ErrorBarSetup import Ui_Define_Plot_Widget
import Plot_Widget
from plotDataClass import LPC


class Define_Error_Plot(QWidget):
    def __init__(self, global_list, local_Dict, varDict, append = False,  parent = None):
        QWidget.__init__(self, None)
        self.ui = Ui_Define_Plot_Widget()
        self.ui.setupUi(self)
        
        self.append = append
        self.parent = parent
        #print self.parent.dialog_test
        self.varDict = varDict
        self.localDict = local_Dict
        self.initialize_setup(global_list,  local_Dict)

        self.connect(self.ui.btn_PlotSelection, SIGNAL("clicked()"),
                     self.getListSelections)
        
    def initialize_setup(self, global_list,  local_Dict):
        if global_list is not None:
#            local_list = []
#            for item in local_Dict.iteritems():
#                try:
#                    if item[1].isLPC():
#                        local_list.append(item[0])
#                except:
#                    print "item not a line plot class"
#                    pass
            local_list = local_Dict.keys()
            self.ui.x_listWidget.addItem('None')
            self.ui.x_listWidget.addItems(global_list)
            self.ui.x_listWidget.setCurrentRow(0)
            self.ui.y_listWidget.addItem('None')
            self.ui.y_listWidget.addItems(global_list)
            self.ui.y_listWidget.setCurrentRow(0)
            self.ui.active_listWidget.addItems(local_list)
            self.ui.active_listWidget.setCurrentRow(0)
    
    def getListSelections(self):
        active_item = str(self.ui.active_listWidget.selectedItems()[0].text())
        x_item = str(self.ui.x_listWidget.selectedItems()[0].text())#gets data in X ListWidget
        y_item = str(self.ui.y_listWidget.selectedItems()[0].text())#gets data in Y ListWidget
        #try:
        if active_item != 'None':
            active_LPC = self.localDict.get(active_item)
            if x_item != 'None':
                x_array = self.varDict.get(x_item)
                active_LPC.set_xerr(x_array,  x_item)
            else:
                active_LPC.clear_xerr()
                
            if y_item != 'None':
                y_array = self.varDict.get(y_item)
                active_LPC.set_yerr(y_array,  y_item)
            else:
                active_LPC.clear_yerr()
        
        self.parent.initialize_plot(active_LPC)
        self.close()
            
        #except:
            #print "Error it retrieving arrays from global dictionary"


        #self.__plotData__(dataDict)
    
    def __plotData__(self, data2plot):
        if self.append is False:
            self.plot_dialog = Plot_Widget(LPC(data2plot),self.parent)
            self.plot_dialog.show()
            self.close()
        else:
            self.parent.add_trace(data2plot)
            self.close()
        
    
    
    def program_close(self):
        sys.exit()

    
if __name__ == "__main__":
    array_list = ["Banana", "Apple", "Elderberry", "Clementine", "Fig",
             "Guava", "Mango", "Honeydew Melon", "Date", "Watermelon",
             "Tangerine", "Ugli Fruit", "Juniperberry", "Kiwi",
             "Lemon", "Nectarine", "Plum", "Raspberry", "Strawberry",
             "Orange"]
            
    app = QApplication(sys.argv)
    plot_setup = Define_Plot_Widget(array_list)
    plot_setup.show()
    sys.exit(app.exec_())

